﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraWaitForm;
using DevExpress.XtraSplashScreen;
using System.Windows.Forms;
using BartonExtension;
using Model;

namespace Backend
{
    public static class Utils
    {
        private const string apiKey = "9cb6cc9c6db7a83ff594423bd5f8c7fa";
        public static void IndicatorShowHide(Form container,bool willShow)
        {
            container.Invoke(new Action(() =>
                {
                    if (willShow)
                    {
                        container.Enabled = false;
                        SplashScreenManager.ShowForm(container, typeof(WaitForm1), true, true, false);
                    }
                    else
                    {
                        container.Enabled = true;
                        SplashScreenManager.CloseForm(false);
                    }
                }));
        }

        public static void search(string query, string outputType, int page_num, int page_size, int scope, string region,Action<List<Mall>,Exception> action)
        {
            List<Mall> malls = new List<Mall>();
            string postUrl = "https://maps.googleapis.com/maps/api/place/textsearch/json?query={0} HK&sensor=false&key=AIzaSyDJNUXBGeVuyFWOPVPSq7HsPmYHCuLd5oM&language=zh-CN"
                             .FormatWith(query);
            HttpPost post = new HttpPost(postUrl, string.Empty);
            post.post((x, err) =>
                {
                    Dictionary<string, object> responseData = x.FromJson<Dictionary<string, object>>();
                    if (responseData["status"].Equals("OK"))
                    {
                        try
                        {
                            List<Dictionary<string, object> > results = responseData["results"].ToString().FromJson<List<Dictionary<string, object>>>();
                            foreach (Dictionary<string, object> result in results)
                            {
                                Dictionary<string, object> geometry = result["geometry"].ToString().FromJson<Dictionary<string, object>>();
                                Dictionary<string, object> location = geometry["location"].ToString().FromJson<Dictionary<string, object>>();
                                Mall mall = new Mall()
                                {
                                    name = result["name"].ToString(),
                                    street = result["formatted_address"].ToString(),
                                    latitude = location["lat"].toFloat().Value,
                                    longitude = location["lng"].toFloat().Value
                                };
                                malls.Add(mall);
                            }
                            action(malls, null);
                        }   
                        catch(Exception ex)
                        {
                            action(null, ex);
                        }
                    }
                    else
                    {
                        Exception ex = new Exception("No data found");
                        action(null, ex);
                    }
                });
        }
        public static double MAX(double digit1, double digit2)
        {
            if (digit1 >= digit2)
                return digit1;
            else return digit2;
        }
        public static int MAX(int digit1, int digit2)
        {
            if (digit1 >= digit2)
                return digit1;
            else return digit2;
        }
        public static float MAX(float digit1, float digit2)
        {
            if (digit1 >= digit2)
                return digit1;
            else return digit2;
        }
        public static double MIN(double digit1, double digit2)
        {
            if (digit1 <= digit2)
                return digit1;
            else return digit2;
        }
        public static int MIN(int digit1, int digit2)
        {
            if (digit1 <= digit2)
                return digit1;
            else return digit2;
        }
        public static float MIN(float digit1, float digit2)
        {
            if (digit1 <= digit2)
                return digit1;
            else return digit2;
        }
    }
}
